<?php
class model_links extends database_mysql
{
    public function mdl_get_list_item($page=1,$limit=1000,$order_by = 'ID')
    {
        $str = null;
        if(!empty($limit))
        {
			$page=($page==0 || empty($page)?1:$page);
			$start = ($page - 1)* $limit; 
			$strlimit = "LIMIT $start, $limit";
		}
		else{
			$strlimit ='';
		}

        $sql = "
            SELECT
            *
            FROM
            ". prefix ."links
            ORDER BY ". prefix ."links.$order_by DESC ".$strlimit;
        $row = parent::mysql_fetch_all_rows($sql);
        parent::mysql_close_connection();
 		return $row;
    }
	public function mdl_save_data($table, $data){
        $table = prefix . $table;
        parent::mysql_insert_array($table, $data);
    }
    
    public function model_update_data($table, $data, $condition){
        $table = prefix . $table;
        parent::mysql_update_array($table, $data, $condition);
    }
    
    public function mdl_tool_func($model,$active,$str_id)
    {
        $done = false;
        switch($active)
        {
            case 'show':
            case 'hide':
                $status = array('show'=>'true', 'hide'=>'false');
                $done = $model->model_tool_set_status(prefix."links",$status[$active]," ID IN(".trim($str_id).")");
                
            break;

            case 'delete':
                $str_id = explode(',',$str_id);
                foreach($str_id as $id)
                {
                    $item = $this->mdl_get_item('links', $id);
                    if(!empty($item) && !empty($item['Picture']))
                    {
                        $path_img = realpath('') . '/upload/images/links/' . $item['Picture'];
                        $info_file = pathinfo($path_img);
                        $file_name = $info_file['filename'];
                        $dir_cache = $info_file['dirname'] . '/cache';
                        $path_thumb = $dir_cache . '/' . '135x135';
                        $file_thumb = $path_thumb . '/' . $info_file['basename'];
                        
                        if(file_exists($path_img))
                        {
                            unlink($path_img);
                            if(file_exists($file_thumb))
                            {
                                unlink($file_thumb);
                            }
                        }
                    }
                    
                    $done = $model->model_tool_delete(prefix."links"," ID IN(".trim($id).")" );
                }
                
                
                
                
            break;
        }

        return $done;
    }
    
    public function mdl_get_item($table,$id){
        $sql = " 
                    SELECT *
                    FROM
                    ". prefix .$table."
                    WHERE
                    ". prefix ."$table.ID = 
        ".$id;
        $row = parent::mysql_fetch_one_rows($sql);
 		return $row;
    }
    
    public function mdl_get_total($table, $cid = null)
    {
        $sql_cid = '';
        if($cid != null && $cid != 'all')
        {
            $sql_cid = " WHERE ". prefix ."$table.CID = $cid";
        }
        $sql = "SELECT COUNT( * ) AS total FROM ". prefix .$table . $sql_cid;
        $result = parent::mysql_select_one($sql);
        return  $result;
    }
}

